package main.servlet;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import main.pojo.User;
import main.servlet.service.VerifyService;
import main.servlet.service.impl.VerifyServiceImpl;
import main.servlet.service.listener.VerifyListener;
import main.util.ParseUtil;

/**
 * 注册信息处理
 */
public class VerifyServlet extends HttpServlet  {
	private static final long serialVersionUID = 1L;
  
    public VerifyServlet() {
        super();
        
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	
	protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
		
//		request.setCharacterEncoding("UTF-8");
		
		
		VerifyServiceImpl service = new VerifyServiceImpl(new VerifyListener() {
			
			@Override
			public void successVerify(User user) {
				request.setAttribute("user", user);
				try {
					request.getRequestDispatcher("/RegisterServlet").forward(request, response);
				} catch (ServletException | IOException e) {
					e.printStackTrace();
				}
			}
			
			@Override
			public void errorDuringVerify(String msg) {
				request.setAttribute("msg", msg);
				try {
					request.getRequestDispatcher("/ErrorMsgServlet").forward(request, response);
				} catch (ServletException | IOException e) {
					e.printStackTrace();
				}
			}
		});

		DiskFileItemFactory factory = new DiskFileItemFactory(102400, new File("D:/temp1/"));///100KB， 临时文件存放区
		ServletFileUpload fileUpload = new ServletFileUpload(factory);
		fileUpload.setSizeMax(1024000);///文件最大为1000kb
		fileUpload.setHeaderEncoding("UTF-8");///设置中文文件名的编码格式
		
		try {
			List<FileItem> items=  fileUpload.parseRequest(request);
			for(FileItem item:items){
				if(item.isFormField()){
					//普通表单域
					System.out.println(item.getFieldName()+"......"+ item.getString());
					if("uname".equals(item.getFieldName())){
						service.verifyUserName(item.getString("UTF-8"));
					}else if("sal".equals(item.getFieldName())){
						service.verifySalary(item.getString());
					}else if("birth".equals(item.getFieldName())){
						service.verifyBirth(item.getString());
					}else if("sex".equals(item.getFieldName())){
						service.verifySex(item.getString());
					}
				}else{
					//文件域
					System.out.println(item.getName()+"........"+item.getSize());
					InputStream fin = item.getInputStream();
					byte[] buf = new byte[(int) item.getSize()];
					int len = 0;
					while((len = fin.read(buf))!=-1){
					}
					fin.close();
					service.setUserHeadPic(buf);
				}
			}
		} catch (FileUploadException e) {
			//上传过程中出现任何问题，都会在这里显示，并提示我们处理
			e.printStackTrace();
		}
		
		
	}

}
